package stackdemo;

import java.util.Arrays;
import java.util.Stack;

public class MyStack {
    public int[] elem;
    public int useSize;
    public static final int DEFAULT_CAPACITY = 10;
    public MyStack(){
        this.elem = new int[DEFAULT_CAPACITY];
    }

    public void push(int val){
        if (isFull()){
//            栈满，扩容
            this.elem = Arrays.copyOf(elem,elem.length*2);
        }
        elem[useSize] = val;
        useSize++;
    }

    public boolean isFull(){
        return useSize == elem.length;
    }
//    出栈
    public int pop(){
        if (isEmpty()){
            return -1;
        }
        int pop = elem[useSize-1];
        useSize--;
        return pop;
    }

    public boolean isEmpty(){
        return useSize==0;
    }

    public int peek(){
        if (isEmpty()){
            throw new EmptyStackException("空栈....");
        }
        return elem[useSize-1];
    }

    public int evalPRN(String[] tokens){
        Stack<Integer> stack = new Stack<>();
//            1.数字入栈
//            2.假如遇到+ * /，第一个弹出数字在右边，第二个在左边，将结果又放回栈中
//            3.栈空结束
        for (int i = 0; i < tokens.length; i++) {
            String str = tokens[i];
            if (!isOperations(str)){
//                string类型转换为整型
                int val = Integer.valueOf(str);
                stack.push(val);
            }else {
                int right = stack.pop();
                int left = stack.pop();
                switch (str){
                    case "+":
                        stack.push(right+left);
                        break;
                    case "-":
                        stack.push(right-left);
                        break;
                    case "*":
                        stack.push(right*left);
                        break;
                    case "/":
                        stack.push(right/left);
                        break;
                }
            }
        }
        return stack.pop();
    }
    /*判断当前字符串是运算符还是数字*/
    private boolean isOperations(String str){
        if (str.equals("+") || str.equals("-") || str.equals("*") || str.equals("/")){
            return true;
        }
        return false;
    }

    public boolean isValid(String s){
        Stack<Character> stack = new Stack<>();
        /*遍历字符串*/
        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            if (ch == '(' || ch == '[' || ch == '{'){
//                是有括号

            }else {
//                不是右括号

            }
        }
    }
}
